package com.lsz.deport.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.common.UIApplication;
import com.common.config.Global;
import com.common.mvp.BaseMvpActivity;
import com.common.widght.IncludeTitleLayout;
import com.common.widght.TabViewBean;
import com.common.widght.TabViewPagerLayout;
import com.common.widght.smarttablayout.support.FragmentPagerItems;
import com.lsz.deport.R;
import com.lsz.deport.dialog.ConfirmDialog;
import com.lsz.deport.json.CheckUpdate;
import com.lsz.deport.json.UserEntity;
import com.lsz.deport.ui.fragment.CompleteFragment;
import com.lsz.deport.ui.fragment.DeliveredFragment;
import com.lsz.deport.ui.fragment.DeliveryFragment;
import com.lsz.deport.ui.login.LoginActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseMvpActivity<MainView, MainPresent> implements MainView {

    @BindView(R.id.itl_toolbar)
    IncludeTitleLayout itlToolbar;
    @BindView(R.id.tl_view_pager)
    TabViewPagerLayout tlViewPager;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_sign_out)
    TextView tvSignOut;

    private ConfirmDialog mConfirmDialog;

    public static void startActivity(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setTranslateStatusBar(boolean barColor) {
        super.setTranslateStatusBar(false);
    }

    @Override
    protected void setupView() {
        itlToolbar.setTitleText("配送任务", R.color.app_white);
        itlToolbar.setTitleBarHeight(View.VISIBLE);

        tvSignOut.setOnClickListener(this::onClick);

        String[] statusList = getResources().getStringArray(R.array.status_list);
        List<TabViewBean> tabViewList = new ArrayList<>();
        DeliveryFragment deliveryF = new DeliveryFragment();
        TabViewBean tabViewBean = new TabViewBean();
        tabViewBean.tabKey = statusList[0];
        tabViewBean.tabValue = deliveryF;
        tabViewList.add(tabViewBean);
        DeliveredFragment deliveredF = new DeliveredFragment();
        TabViewBean tabViewBean1 = new TabViewBean();
        tabViewBean1.tabKey = statusList[1];
        tabViewBean1.tabValue = deliveredF;
        tabViewList.add(tabViewBean1);
        CompleteFragment completeFt = new CompleteFragment();
        TabViewBean tabViewBean2 = new TabViewBean();
        tabViewBean2.tabKey = statusList[2];
        tabViewBean2.tabValue = completeFt;
        tabViewList.add(tabViewBean2);
        tlViewPager.setFragmentPagerItem(tabViewList);
        tlViewPager.setPagerListener(pageChangeListener);
        tlViewPager.setPagerItemListener(pagerItemListener);
    }


    @Override
    protected void setupData(Bundle savedInstanceState) {
        UserEntity userBean = Global.getUserBean();
        String name = "配送员：" + userBean.outerman.name;
        tvUserName.setText(name);
    }

    public void onClick(View view) {
        ConfirmDialog.Builder builder = new ConfirmDialog.Builder(this);
        mConfirmDialog = builder
                .setContentText("是否退出登录？")
                .setConfirmListener(new ConfirmDialog.Builder.ConfirmClickListener() {
                    @Override
                    public void click(boolean b) {
                        mConfirmDialog.dismiss();
                        if (b) {
                            /*--清空所有页面--*/
                            UIApplication.getAppManager().finishAllActivity();
                            Global.loginOutclear();
                            LoginActivity.startActivity(MainActivity.this);
                        }
                    }
                }).create();
        mConfirmDialog.show();
    }

    @Override
    public void OnVersionUpdate(CheckUpdate result) {
    }

    @Override
    public void OnVersionUpdateError() {
    }

    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
//            if (position == 0) {
//                getPresenter().getOrderList(0, null, null, "processing");
//            } else if (position == 1) {
//                getPresenter().getOrderList(1, null, null, "completed");
//            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    private TabViewPagerLayout.PagerItemListener pagerItemListener = new TabViewPagerLayout.PagerItemListener() {
        @Override
        public void onFragmentPagerItems(FragmentPagerItems fragmentPagerItems) {
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return mDoubleClickExitHelper.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }
}
